﻿using Microsoft.Extensions.DependencyInjection;
namespace Athena.Transport.DotNetty
{
    using Runtime.Server;
    using Runtime.Server.Implementations;
    public static class DotNettyTransportExtension
    {
        public static IServiceCollection AddDotNettyTransport(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddSingleton<ITransportClientFactory, DotNettyTransportClientFactory>();

  serviceCollection.AddSingleton<DotNettyTransportMessageListener>();
            serviceCollection.AddSingleton<ISerivceHost, ServiceHost>(provider =>
            {
                var serivceExecutor = provider.GetRequiredService<IServiceExecutor>();
                var dotNettyTransportMessageListener = provider.GetRequiredService<DotNettyTransportMessageListener>();

                return new ServiceHost(async endpont =>
                {
                    await dotNettyTransportMessageListener.StartAsync(endpont);
                    return dotNettyTransportMessageListener;
                }, serivceExecutor);
            });

            return serviceCollection;
        }
    }
}
